package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.BrandMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {


    @Override
    public Result findAllBrand() {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.select();
        return new Result(true, StatusCode.OK,null,baseMapper.selectList(wrapper));
    }

    @Override
    public Result addBrand(Brand brand) {
        int insert = baseMapper.insert(brand);
        return new Result(true,StatusCode.OK,"添加成功",null);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        Page<Brand> page1 = new Page<>(page,size);
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
            wrapper.select();
          baseMapper.selectPage(page1,wrapper);
        PageResult<Brand> result = new PageResult<>();
        result.setTotal(page1.getTotal());
        result.setRows(page1.getRecords());
        return new Result(true,StatusCode.OK,null,result);

    }

    @Override
    public Result findById(Integer id) {
        return new Result(true,StatusCode.OK,null, baseMapper.selectById(id));
    }

    @Override
    public Result updateBrand(Integer id, Brand brand) {
        UpdateWrapper<Brand> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        baseMapper.update(brand,wrapper);
        return new Result(true,StatusCode.OK,"更新成功",null);
    }

    @Override
    public Result deleteById(Integer id) {
        baseMapper.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功",null);
    }
}
